//
//  ContactCell.swift
//  com.yunshu.yunshuapp
//
//  Created by 周文杰 on 2017/6/14.
//  Copyright © 2017年 zwj. All rights reserved.
//

import UIKit

class ContactCell: UITableViewCell {

    let titleLabel = UILabel()
    let arrow = UIImageView()
    let line = UIView()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.titleLabel.font = UIFont.systemFont(ofSize: 14)
        self.titleLabel.textColor = UIColor.black
        self.titleLabel.numberOfLines = 0
        self.contentView.addSubview(self.titleLabel)
        
        self.arrow.frame = CGRect.init(x: Const.SCREEN_WIDTH - 40, y: 10, width: 30, height: 30)
        self.arrow.image = UIImage.init(named: "right_arrow")
        self.contentView.addSubview(self.arrow)
        
        self.line.backgroundColor = Const.WTM_LIGHTGRAY
        self.contentView.addSubview(self.line)
        
        self.selectionStyle = .none
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func setTitle(_ text: String) {
        self.titleLabel.text = text
        let width = (Const.SCREEN_WIDTH - 40 )
        
        var height = UIUtility.calcStringHeight(text, font: self.titleLabel.font, refWidth: width)
        if height < 30  {
            height = 30
        }
        self.titleLabel.frame = CGRect.init(x: 10, y: 10, width: width, height: height)
        
        var center = self.arrow.center
        center.y = (height + 20) / 2
        
        self.line.frame = CGRect.init(x: 0, y: height + 20 - Const.ONE_PIXEL, width: Const.SCREEN_WIDTH, height: Const.ONE_PIXEL)
    }
    
    static func heightWith(_ text:String) -> CGFloat {
        
        let width = (Const.SCREEN_WIDTH - 60 )
        
        var height = UIUtility.calcStringHeight(text, font: UIFont.systemFont(ofSize: 14), refWidth: width)
        if height < 30  {
            height = 30
        }
        return height + 20
    }

}
